package com.sudo.api._admin;

import com.sudo.common.utils.HttpUserInfoUtil;
import com.sudo.common.utils.JsonRes;
import com.sudo.common.utils.PagedGridResult;
import com.sudo.dao.admin.model.vo.StsMyBehaviorVO;
import com.sudo.dao.admin.model.vo.StsUserBehaviorVO;
import com.sudo.dao.admin.entity.SysMyStsDay;
import com.sudo.dao.admin.entity.SysMyStsMonth;
import com.sudo.dao.admin.entity.SysUserStsDay;
import com.sudo.dao.admin.entity.SysUserStsMonth;
import com.sudo.service.admin.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.util.List;

@Api(value = "用户行为统计",tags = {"统计登录操作信息接口"})
@RestController
@RequestMapping("userBehavior")
public class StsUserBehaviorController extends AdminBaseController {
    @Resource
    private SysUserStsDayService sysUserStsDayService;

    @Resource
    private SysUserStsMonthService sysUserStsMonthService;

    @Resource
    private SysMyStsDayService sysMyStsDayService;

    @Resource
    private SysMyStsMonthService sysMyStsMonthService;

    @Resource
    private SysUserBehaviorService sysUserBehaviorService;

    @Resource
    private SysUserLoginRecordService sysUserLoginRecordService;

    @ApiOperation(value = "分页查询用户行为记录",notes = "分页查询用户行为记录",httpMethod = "GET")
    @GetMapping("/queryUserBehaviorByPage")
    public JsonRes queryUserBehaviorByPage(@RequestParam Integer page,
                                           @RequestParam Integer pageSize,
                                           @RequestParam String username ,
                                           @RequestParam String startDate,
                                           @RequestParam String endDate) {
        PagedGridResult pagedGridResult = sysUserBehaviorService.queryUserBehaviorByPage(page, pageSize, username,startDate,endDate);
        return JsonRes.ok(pagedGridResult);
    }

    @ApiOperation(value = "分页查询我的行为记录",notes = "分页查询我的行为记录",httpMethod = "GET")
    @GetMapping("/queryMyBehaviorByPage")
    public JsonRes queryMyBehaviorByPage(@RequestParam Integer page,
                                         @RequestParam Integer pageSize,
                                         HttpServletRequest request) {
        String sessionUser = HttpUserInfoUtil.getSessionUser(request);
        PagedGridResult pagedGridResult = sysUserBehaviorService.queryUserBehaviorByPage(page, pageSize, sessionUser,null,null);
        return JsonRes.ok(pagedGridResult);
    }

    @ApiOperation(value = "分页查询用户登录记录",notes = "分页查询用户登录记录",httpMethod = "GET")
    @GetMapping("/queryUserLoginRecordByPage")
    public JsonRes queryUserLoginRecordByPage(@RequestParam Integer page,
                                              @RequestParam Integer pageSize,
                                              @RequestParam String username ,
                                              @RequestParam String startDate,
                                              @RequestParam String endDate) {
        PagedGridResult pagedGridResult = sysUserLoginRecordService.queryUserLoginRecordByPage(page, pageSize, username,startDate,endDate);
        return JsonRes.ok(pagedGridResult);
    }

    @ApiOperation(value = "分页查询我的登录记录",notes = "分页查询我的登录记录",httpMethod = "GET")
    @GetMapping("/queryMyLoginRecordByPage")
    public JsonRes queryMyLoginRecordByPage(@RequestParam Integer page,
                                            @RequestParam Integer pageSize,
                                            HttpServletRequest request) {
        String sessionUser = HttpUserInfoUtil.getSessionUser(request);
        PagedGridResult pagedGridResult = sysUserLoginRecordService.queryUserLoginRecordByPage(page, pageSize, sessionUser,null,null);
        return JsonRes.ok(pagedGridResult);
    }

    @ApiOperation(value = "获取近30天用户登录活跃度",notes = "获取近30天用户登录活跃度",httpMethod = "GET")
    @GetMapping("/getSysUserStsDayList")
    public JsonRes getSysUserStsDayList() {
        LocalDate endLocalDate = LocalDate.now();
        LocalDate startLocalDate = endLocalDate.minusDays(30);
        List<SysUserStsDay> sysUserStsDayList = sysUserStsDayService.getSysUserStsDayList(startLocalDate,endLocalDate);
        return JsonRes.ok(sysUserStsDayList);
    }

    @ApiOperation(value = "获取近12月用户登录活跃度",notes = "获取近12月用户登录活跃度",httpMethod = "GET")
    @GetMapping("/getSysUserStsMonthList")
    public JsonRes getSysUserStsMonthList() {
        LocalDate localDate = LocalDate.now();
        List<SysUserStsMonth> sysUserStsMonthList = sysUserStsMonthService.getSysUserStsMonthList(localDate);
        return JsonRes.ok(sysUserStsMonthList);
    }

    @ApiOperation(value = "获取首页用户统计数据",notes = "获取首页用户统计数据",httpMethod = "GET")
    @GetMapping("/getStsUserBehavior")
    public JsonRes getStsUserBehavior() {
        LocalDate today = LocalDate.now();
        LocalDate yesterday = today.minusDays(1);

        int thisMonthOfYear = today.getYear();
        int thisMonth = today.getMonthValue();

        LocalDate lastMonthDate = today.minusMonths(1);
        int lastMonthOfYear = lastMonthDate.getYear();
        int lastMonth = lastMonthDate.getMonthValue();

        StsUserBehaviorVO stsUserBehaviorVO = new StsUserBehaviorVO();
        SysUserStsDay todayUserStsDay = sysUserStsDayService.getUserStsDay(today);
        if (todayUserStsDay == null) {
            stsUserBehaviorVO.setTodayLoginNop(0);
            stsUserBehaviorVO.setTodayLoginNot(0);
            stsUserBehaviorVO.setTodayNumBehavior(0);
        }else{
            stsUserBehaviorVO.setTodayLoginNop(todayUserStsDay.getLoginNop());
            stsUserBehaviorVO.setTodayLoginNot(todayUserStsDay.getLoginNot());
            stsUserBehaviorVO.setTodayNumBehavior(todayUserStsDay.getNumBehavior());
        }

        SysUserStsDay yesterdayUserStsDay = sysUserStsDayService.getUserStsDay(yesterday);
        if (yesterdayUserStsDay == null) {
            stsUserBehaviorVO.setYesterdayLoginNop(0);
            stsUserBehaviorVO.setYesterdayLoginNot(0);
            stsUserBehaviorVO.setYesterdayNumBehavior(0);
        }else {
            stsUserBehaviorVO.setYesterdayLoginNop(yesterdayUserStsDay.getLoginNop());
            stsUserBehaviorVO.setYesterdayLoginNot(yesterdayUserStsDay.getLoginNot());
            stsUserBehaviorVO.setYesterdayNumBehavior(yesterdayUserStsDay.getNumBehavior());
        }

        SysUserStsMonth thisMonthUserStsMonth = sysUserStsMonthService.getUserStsMonth(thisMonthOfYear,thisMonth);
        if (thisMonthUserStsMonth == null) {
            stsUserBehaviorVO.setThisMonthLoginNop(0);
            stsUserBehaviorVO.setThisMonthLoginNot(0);
            stsUserBehaviorVO.setThisMonthNumBehavior(0);
        }else {
            stsUserBehaviorVO.setThisMonthLoginNop(thisMonthUserStsMonth.getLoginNop());
            stsUserBehaviorVO.setThisMonthLoginNot(thisMonthUserStsMonth.getLoginNot());
            stsUserBehaviorVO.setThisMonthNumBehavior(thisMonthUserStsMonth.getNumBehavior());
        }

        SysUserStsMonth lastMonthUserStsMonth = sysUserStsMonthService.getUserStsMonth(lastMonthOfYear,lastMonth);
        if (lastMonthUserStsMonth == null){
            stsUserBehaviorVO.setLastMonthLoginNop(0);
            stsUserBehaviorVO.setLastMonthLoginNot(0);
            stsUserBehaviorVO.setLastMonthNumBehavior(0);
        }else {
            stsUserBehaviorVO.setLastMonthLoginNop(lastMonthUserStsMonth.getLoginNop());
            stsUserBehaviorVO.setLastMonthLoginNot(lastMonthUserStsMonth.getLoginNot());
            stsUserBehaviorVO.setLastMonthNumBehavior(lastMonthUserStsMonth.getNumBehavior());
        }

        return JsonRes.ok(stsUserBehaviorVO);
    }



    @ApiOperation(value = "获取近30天我的登录活跃度",notes = "获取近30天我的登录活跃度",httpMethod = "GET")
    @GetMapping("/getSysMyStsDayList")
    public JsonRes getSysMyStsDayList(HttpServletRequest request) {
        String sessionUser = HttpUserInfoUtil.getSessionUser(request);
        LocalDate localDate = LocalDate.now();
        List<SysMyStsDay> sysMyStsDayList = sysMyStsDayService.getSysMyStsDayList(localDate,sessionUser);
        return JsonRes.ok(sysMyStsDayList);
    }

    @ApiOperation(value = "获取近12个月我的登录活跃度",notes = "获取近12个月我的登录活跃度",httpMethod = "GET")
    @GetMapping("/getSysMyStsMonthList")
    public JsonRes getSysMyStsMonthList(HttpServletRequest request) {
        String sessionUser = HttpUserInfoUtil.getSessionUser(request);
        LocalDate localDate = LocalDate.now();
        List<SysMyStsMonth> sysMyStsMonthList = sysMyStsMonthService.getSysMyStsMonthList(localDate,sessionUser);
        return JsonRes.ok(sysMyStsMonthList);
    }

    @ApiOperation(value = "获取首页我的统计数据",notes = "获取首页我的统计数据",httpMethod = "GET")
    @GetMapping("/getStsMyBehavior")
    public JsonRes getStsMyBehavior(HttpServletRequest request) {
        String sessionUser = HttpUserInfoUtil.getSessionUser(request);
        LocalDate today = LocalDate.now();
        LocalDate yesterday = today.minusDays(1);

        int thisMonthOfYear = today.getYear();
        int thisMonth = today.getMonthValue();

        LocalDate lastMonthDate = today.minusMonths(1);
        int lastMonthOfYear = lastMonthDate.getYear();
        int lastMonth = lastMonthDate.getMonthValue();

        StsMyBehaviorVO stsMyBehaviorVO = new StsMyBehaviorVO();
        SysMyStsDay todayMyStsDay = sysMyStsDayService.getMyStsDay(today,sessionUser);
        if (todayMyStsDay == null) {
            stsMyBehaviorVO.setTodayLoginNot(0);
            stsMyBehaviorVO.setTodayNumBehavior(0);
        }else{
            stsMyBehaviorVO.setTodayLoginNot(todayMyStsDay.getLoginNot());
            stsMyBehaviorVO.setTodayNumBehavior(todayMyStsDay.getNumBehavior());
        }

        SysMyStsDay yesterdayMyStsDay = sysMyStsDayService.getMyStsDay(yesterday,sessionUser);
        if (yesterdayMyStsDay == null) {
            stsMyBehaviorVO.setYesterdayLoginNot(0);
            stsMyBehaviorVO.setYesterdayNumBehavior(0);
        }else {
            stsMyBehaviorVO.setYesterdayLoginNot(yesterdayMyStsDay.getLoginNot());
            stsMyBehaviorVO.setYesterdayNumBehavior(yesterdayMyStsDay.getNumBehavior());
        }

        SysMyStsMonth thisMonthMyStsMonth = sysMyStsMonthService.getMyStsMonth(thisMonthOfYear,thisMonth,sessionUser);
        if (thisMonthMyStsMonth == null) {
            stsMyBehaviorVO.setThisMonthLoginNot(0);
            stsMyBehaviorVO.setThisMonthNumBehavior(0);
        }else {
            stsMyBehaviorVO.setThisMonthLoginNot(thisMonthMyStsMonth.getLoginNot());
            stsMyBehaviorVO.setThisMonthNumBehavior(thisMonthMyStsMonth.getNumBehavior());
        }

        SysMyStsMonth lastMonthMyStsMonth = sysMyStsMonthService.getMyStsMonth(lastMonthOfYear,lastMonth,sessionUser);
        if (lastMonthMyStsMonth == null){
            stsMyBehaviorVO.setLastMonthLoginNot(0);
            stsMyBehaviorVO.setLastMonthNumBehavior(0);
        }else {
            stsMyBehaviorVO.setLastMonthLoginNot(lastMonthMyStsMonth.getLoginNot());
            stsMyBehaviorVO.setLastMonthNumBehavior(lastMonthMyStsMonth.getNumBehavior());
        }

        return JsonRes.ok(stsMyBehaviorVO);
    }
}
